package com.hemaapp.yjnh.adapter;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import xtom.frame.image.load.XtomImageTask;
import xtom.frame.view.XtomListView;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.hemaapp.yjnh.BaseAdapter;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.activity.BookProductionDetailActivity;
import com.hemaapp.yjnh.bean.Blog;

/**村游商家详情下的村游商品
 * @author GuiyouZhang
 * Date: 2015-09-10
 */
public class CunyouListAdapter extends BaseAdapter {

	private ArrayList<Blog> zouzous;
	private Context mContext;
	private XtomListView listView;
	
	public CunyouListAdapter(Context mContext, ArrayList<Blog> zouzous, XtomListView listView) {
		super(mContext);
		this.zouzous = zouzous;
		this.mContext = mContext;
		this.listView = listView;
	}

	@Override
	public int getCount() {
		return zouzous.size() == 0 ? 1 : zouzous.size();
	}
	
	@Override
    public boolean isEmpty() {
        int size = zouzous == null? 0:zouzous.size();
        return size == 0;
    }

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (isEmpty()) 
			return getEmptyView(parent);
		ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.listitem_zouzou_footprint, null);
            holder = new ViewHolder();
            findView(convertView, holder);
            convertView.setTag(R.id.TAG_VIEWHOLDER, holder);
        }
		setData(position, convertView);
		return convertView;
	}

	private class ViewHolder {
		private ImageView ivZouzou;
		private TextView tvDescription;
		private TextView tvPrice;
		private TextView tvReply;
		private RatingBar ratingBar;
	}
	
	private void findView(View convertView, ViewHolder holder) {
		holder.ivZouzou = (ImageView)convertView.findViewById( R.id.iv_zouzou );
		holder.tvDescription = (TextView)convertView.findViewById( R.id.tv_description );
		holder.tvPrice = (TextView)convertView.findViewById( R.id.tv_price );
		holder.tvReply = (TextView)convertView.findViewById( R.id.tv_reply );
		holder.ratingBar = (RatingBar)convertView.findViewById( R.id.ratingBar );
	}
	
	private void setData(final int position, View convertView) {
		final Blog zouzou = zouzous.get(position);
		ViewHolder holder = (ViewHolder) convertView.getTag(R.id.TAG_VIEWHOLDER);
		try {
			XtomImageTask task = new XtomImageTask(holder.ivZouzou, new URL(zouzou
					.getImgurl()), mContext);
			listView.addTask(position, 0, task);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		holder.tvDescription.setText(zouzou.getName());
		holder.tvPrice.setText("¥"+zouzou.getPrice());
		holder.tvReply.setText(zouzou.getReplycount()+"人点评");
		float score = Float.parseFloat(zouzou.getStarscore());
		holder.ratingBar.setRating(score);

		convertView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent itZouzou = new Intent(mContext, BookProductionDetailActivity.class);
				itZouzou.putExtra("blog_id", zouzou.getId());
				mContext.startActivity(itZouzou);
			}
		});
	}
}
